#include <iostream>
#include <thread>
#include <mutex>

using std::cout;
using std::endl;
using std::thread;
using std::mutex;

int gCnt = 0;
mutex mtx;//创建互斥锁

//任何一个线程都将gCnt这样一个全局变量加100w次
void threadFunc()
{
    for(size_t idx = 0; idx != 1000000; ++idx)
    {
        mtx.lock();//上锁
        ++gCnt;
        mtx.unlock();//解锁
    }
}
//线程的执行是随机的
int main(int argc, char *argv[])
{
    thread thA(threadFunc);
    thread thB(threadFunc);

    thA.join();
    thB.join();

    cout << "gCnt = " << gCnt << endl;

    return 0;
}

